package com.nanhang.service.impl;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nanhang.entity.Specification;
import com.nanhang.entity.SpecificationOption;
import com.nanhang.entity.SpecificationOptionExample;
import com.nanhang.mapper.SpecificationMapper;
import com.nanhang.mapper.SpecificationOptionMapper;
import com.nanhang.service.SpecificationOptionService;
import com.nanhang.util.PageResult;
import com.nanhang.vo.SpecificationOptionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationOptionServiceImpl implements SpecificationOptionService {

    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    @Autowired
    private SpecificationMapper specificationMapper;
    @Override
    public List<SpecificationOption> findAll() {
        List<SpecificationOption> specificationOptions = specificationOptionMapper.selectByExample(null);
        specificationOptions.forEach(st -> {
            Specification specification = specificationMapper.selectByPrimaryKey(st.getSpecId());
            st.setSpecName(specification.getSpecName());
        });
        return specificationOptions;
    }

    @Override
    public PageResult<SpecificationOption> findByPage(Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        List<SpecificationOption> specificationOptions = this.findAll();
        Page<SpecificationOption> specificationOptionPage = (Page<SpecificationOption>) specificationOptions;
        return new PageResult<>(specificationOptionPage.getResult(),specificationOptionPage.getTotal());
    }


    @Override
    public PageResult<SpecificationOption> search(Integer page, Integer pageSize, SpecificationOptionVO specificationOptionvo) {
        // 1. 开始分页
        System.out.println("后端search");
        PageHelper.startPage(page, pageSize);
        // 2. 查询数据
        // 2.1 定义一次查询的实例
        SpecificationOptionExample example = new SpecificationOptionExample();
        // 2.2 添加查询条件到查询实例中
        SpecificationOptionExample.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotBlank(specificationOptionvo.getOptionName())){
            criteria.andOptionNameLike("%" + specificationOptionvo.getOptionName() + "%");
        }
        if(specificationOptionvo.getSpecId()!= null && specificationOptionvo.getSpecId() != 0){
            criteria.andSpecIdEqualTo(specificationOptionvo.getSpecId());
        }
        if(specificationOptionvo.getOrders() != null && specificationOptionvo.getOrders() != 0){
            criteria.andOrdersEqualTo(specificationOptionvo.getOrders());
        }
        List<SpecificationOption> specificationOptions = specificationOptionMapper.selectByExample(example);

        specificationOptions.forEach(st -> {
            System.out.println("......................................");

            Specification specification = specificationMapper.selectByPrimaryKey(st.getSpecId());

            st.setSpecName(specification.getSpecName());
        });

        Page<SpecificationOption> specificationOptionPage = (Page<SpecificationOption>) specificationOptions;
        return new PageResult<>(specificationOptionPage.getResult(),specificationOptionPage.getTotal());
    }

    @Override
    public boolean save(SpecificationOption specificationOption) {
        return specificationOptionMapper.insert(specificationOption) > 0;
    }
//
//    @Override
//    public boolean save(SpecificationOption specificationOption) {
//        return false;
//    }
}
